//
//  VideoViewController.m
//  HumorousScripts
//
//  Created by 李欢 on 15/6/15.
//  Copyright (c) 2015年 李欢. All rights reserved.
//

#import "VideoViewController.h"
#import <AFNetworking/AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>
#import "VideoTableViewCell.h"
#import "PlayViewController.h"
#import "Player.h"
#import "MJRefresh.h"
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height

@interface VideoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *bigArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) int i;
@end

@implementation VideoViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.bigArray = [NSMutableArray array];
        
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self startConnection];
}
#pragma mark -
#pragma mark 请求数据
- (void)startConnection
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];

    NSString *str = @"http://ic.snssdk.com/neihan/stream/mix/v1/?tag=joke&iid=2808432718&content_type=-104&min_time=1434354902&count=30&message_cursor=0&ac=WIFI&channel=tongbu&app_name=joke_essay&aid=7&version_code=4.1.0&device_platform=iphone&os_version=8.3&device_type=iPhone%205C&device_id=4271461677&vid=829E0614-C370-4377-9F9B-30FD8109F22F&openudid=fca6cd442f6c85a2b95bc876dcea5652ff538286&os_api=18&idfa=267C5897-7EF8-4BBC-90CE-170DFAABF5CA&screen_width=640";
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        NSDictionary *secondDic = [dic objectForKey:@"data"];
        self.bigArray = [secondDic objectForKey:@"data"];
    
        [self MyTableView];
        [self.tableView reloadData];
    

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
    }];
    
}

//创建tableView
- (void)MyTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64/667.0*Height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //设定上啦加载 下拉刷新
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData:)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

    [self.view addSubview:_tableView];
}
//下拉刷新点击事件
- (void)loadNewData:(id)sender
{
    //    self.i = 2;
    [self.bigArray removeAllObjects];
    [self.tableView reloadData];
    
    [self startConnection];
    [self.tableView.header endRefreshing];
}
//上啦加载点击事件
- (void)loadMoreData
{
    //网络请求 page值 i++
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *str1 = @"&ac=WIFI&channel=tongbu&app_name=joke_essay&aid=7&version_code=4.1.0&device_platform=iphone&os_version=8.3&device_type=iPhone%205C&device_id=4271461677&vid=829E0614-C370-4377-9F9B-30FD8109F22F&openudid=fca6cd442f6c85a2b95bc876dcea5652ff538286&os_api=18&idfa=267C5897-7EF8-4BBC-90CE-170DFAABF5CA&screen_width=640";
    NSString *str = [NSString stringWithFormat:@"http://ic.snssdk.com/neihan/stream/mix/v1/?tag=joke&iid=2808432718&content_type=-104&min_time=1434354902&count=30&message_cursor=%d%@", self.i, str1];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        NSDictionary *secondDic = [dic objectForKey:@"data"];
        NSArray *array = [secondDic objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            [self.bigArray addObject:dic];
        }
        
        [self.tableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // 拿到当前的上拉刷新控件，结束刷新状态
    
    self.i += 1;
    [self.tableView.footer endRefreshing];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bigArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentify = @"cell";
    VideoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[VideoTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
    }
    NSDictionary *dic = [self.bigArray objectAtIndex:indexPath.row];
    NSDictionary *dic1 = [dic objectForKey:@"group"];
    NSDictionary *dic4 = [dic1 objectForKey:@"origin_video"];

    NSDictionary *dic2 = [dic1 objectForKey:@"user"];
    
    cell.x = [[dic4 objectForKey:@"width"] integerValue];
    cell.y = [[dic4 objectForKey:@"height"] integerValue];
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:[dic2 objectForKey:@"avatar_url"]] placeholderImage:nil options:SDWebImageRetryFailed];
    cell.alabel.text = [dic2 objectForKey:@"name"];
    cell.wordsLabel.text = [dic1 objectForKey:@"title"];
    [cell setAlabelFrame];
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
//    NSDictionary * gDic = [self.bigArray objectAtIndex:indexPath.row];
//    NSDictionary * sgDic = [gDic objectForKey:@"group"];
//    NSDictionary *dic4 = [sgDic objectForKey:@"origin_video"];

    return 80/667.0*Height + 200/667.0*Height + 30;

}

#pragma mark -
#pragma mark 点击协议

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //使cell不会保持持续选中状态
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    播放视频页面
    NSDictionary *dic = [self.bigArray objectAtIndex:indexPath.row];
    NSDictionary *dic1 = [dic objectForKey:@"group"];
    PlayViewController * play = [[PlayViewController alloc]init];
    play.url = [dic1 objectForKey:@"mp4_url"];
    [self.navigationController pushViewController:play animated:YES];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
